package com.module.third.qq.model

import cn.leancloud.LCObject
import cn.leancloud.LCQuery
import com.module.frame.base.BaseFrameViewModel
import com.module.frame.ext.postEvent
import com.module.third.bean.ThirdAccountBean
import com.module.third.ext.request
import com.module.third.ext.request2
import com.module.third.learcloud.lcQuerySingle
import com.module.third.qq.bean.QQUserInfo
import com.module.third.service.http.getUserInfoService
import kotlinx.coroutines.flow.MutableSharedFlow

class QQViewModel : BaseFrameViewModel() {

    /**
     * qqId获取成功
     */
    var qqIdFlow: MutableSharedFlow<String> = MutableSharedFlow()

    /**
     * 用户信息获取成功
     */
    var infoFlow: MutableSharedFlow<QQUserInfo> = MutableSharedFlow()

    /**
     * 失败
     */
    var failFlow: MutableSharedFlow<String> = MutableSharedFlow()

    fun getQQAppId() {
        request({
            val tClass = ThirdAccountBean::class.java
            val query = LCQuery<LCObject>(tClass.simpleName)
            lcQuerySingle(query, tClass)
        }, {
            if (it != null) {
                postEvent(qqIdFlow, it.qqId)
            } else {
                postEvent(failFlow, "貌似出了点差错，请稍后重试~")
            }
        }, {
            postEvent(failFlow, "貌似出了点差错，请稍后重试~")
        }, true)

    }

    fun getQQInfo(token: String?, QQ_APP_ID: String?, openId: String?) {
        request2({
            getUserInfoService().getQQInfo(token, QQ_APP_ID, openId)
        }, {
            if (it != null) {
                postEvent(infoFlow, it)
            } else {
                postEvent(failFlow, "貌似出了点差错，请稍后重试~")
            }
        }, {
            postEvent(failFlow, "貌似出了点差错，请稍后重试~")
        }, true)
    }
}